<template>
  <div class="hotel-select-page">
    <!-- 页面头部 - 已按比例放大并与下方盒子宽度对齐 -->
    <div class="page-header">
      <!-- <div class="logo-container">
        <img src="@/assets/login-logo.png" alt="Logo" class="page-logo" />
        <h1 class="page-title">
          <img
            src="@/assets/word3.png"
            alt="Title"
            style="width: 100%; height: 100%"
          />
        </h1>
      </div> -->
    </div>

    <!-- 主体内容区 -->
    <div class="page-container">
      <div class="form-wrapper">
        <!-- 卡片容器 -->
        <div class="form-card">
          <div class="form-header">
            <div class="header-decoration"></div>
            <h2 class="form-title">Select Your Hotel</h2>
            <p class="form-subtitle">Choose any of the following hotels</p>
          </div>

          <div class="form-body">
            <!-- 酒店列表（添加加载状态） -->
            <div v-if="isLoading" class="loading-state">
              <i class="fas fa-spinner fa-spin"></i>
              <span>Loading hotels...</span>
            </div>

            <div v-else class="hotel-list">
              <div
                v-for="hotel in sortedHotelList"
                :key="hotel.hotelId"
                :class="[
                  'hotel-item',
                  { active: selectedHotelId === hotel.hotelId },
                ]"
                @click="selectedHotelId = hotel.hotelId"
              >
                <!-- 酒店图标 -->
                <div class="hotel-icon">
                  <i class="fas fa-hotel"></i>
                </div>

                <!-- 酒店信息（按重要性分层显示） -->
                <div class="hotel-info">
                  <div class="hotel-name">{{ hotel.hotelName }}</div>
                  <div class="hotel-address">{{ hotel.address }}</div>
                  <div class="hotel-details">
                    <span class="detail-item location">
                      <img
                        src="@/assets/country.png"
                        alt=""
                        style="width: 20px; height: 20px"
                      />
                      {{ hotel.city }}, {{ hotel.country }}
                    </span>
                    <span class="detail-item brand">
                      <i class="fas fa-tag"></i>
                      {{ hotel.brand || "No Brand" }}
                    </span>
                    <span class="detail-item star" v-if="hotel.star">
                      <img
                        src="@/assets/star.png"
                        alt=""
                        style="width: 20px; height: 20px"
                      />
                      {{ hotel.star }}
                    </span>
                  </div>
                </div>

                <!-- 选中状态标识 -->
                <div
                  class="select-checkbox"
                  :class="{ checked: selectedHotelId === hotel.hotelId }"
                >
                  <i
                    class="el-icon-check"
                    v-if="selectedHotelId === hotel.hotelId"
                  ></i>
                </div>
              </div>
            </div>
          </div>

          <!-- 底部按钮 -->
          <div class="form-footer">
            <button
              type="button"
              class="submit-btn"
              :disabled="!selectedHotelId || isLoading"
              @click="confirmSelect"
            >
              Confirm & Continue
            </button>
          </div>
        </div>

        <div class="help-link">
          <a href="mailto:info@wheretoknow.com" target="_blank">
            <!-- 图标部分 -->
            <span class="icon">
              <!-- <svg
                aria-hidden="true"
                class="email-icon"
                viewBox="0 0 512 512"
                xmlns="http://www.w3.org/2000/svg"
              >
                <path
                  d="M494.586 164.516c-4.697-3.883-111.723-89.95-135.251-108.657C337.231 38.191 299.437 0 256 0c-43.205 0-80.636 37.717-103.335 55.859-24.463 19.45-131.07 105.195-135.15 108.549A48.004 48.004 0 0 0 0 201.485V464c0 26.51 21.49 48 48 48h416c26.51 0 48-21.49 48-48V201.509a48 48 0 0 0-17.414-36.993zM464 458a6 6 0 0 1-6 6H54a6 6 0 0 1-6-6V204.347c0-1.813.816-3.526 2.226-4.665 15.87-12.814 108.793-87.554 132.364-106.293C200.755 78.88 232.398 48 256 48c23.693 0 55.857 31.369 73.41 45.389 23.573 18.741 116.503 93.493 132.366 106.316a5.99 5.99 0 0 1 2.224 4.663V458zm-31.991-187.704c4.249 5.159 3.465 12.795-1.745 16.981-28.975 23.283-59.274 47.597-70.929 56.863C336.636 362.283 299.205 400 256 400c-43.452 0-81.287-38.237-103.335-55.86-11.279-8.967-41.744-33.413-70.927-56.865-5.21-4.187-5.993-11.822-1.745-16.981l15.258-18.528c4.178-5.073 11.657-5.843 16.779-1.726 28.618 23.001 58.566 47.035 70.56 56.571C200.143 320.631 232.307 352 256 352c23.602 0 55.246-30.88 73.41-45.389 11.994-9.535 41.944-33.57 70.563-56.568 5.122-4.116 12.601-3.346 16.778 1.727l15.258 18.526z"
                ></path>
              </svg> -->
            </span>
            <!-- 文本部分 -->
            <span class="text">Need assistance? Contact support</span>
          </a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { fetchHotelList } from "@/api/information.js";
import router from "@/router";

export default {
  name: "HotelSelectPage",
  data() {
    return {
      isLoading: true,
    };
  },
  computed: {
    ...mapState("hotel", ["hotelList"]),
    selectedHotelId: {
      get() {
        return this.$store.state.hotel.currentHotelId;
      },
      set(val) {
        this.SET_CURRENT_HOTEL(val);
      },
    },
    sortedHotelList() {
      return [...this.hotelList].sort((a, b) => {
        return a.hotelName.localeCompare(b.hotelName, undefined, {
          sensitivity: "base",
        });
      });
    },
  },
  methods: {
    ...mapMutations("hotel", ["SET_CURRENT_HOTEL"]),
    confirmSelect() {
      if (this.selectedHotelId) {
        this.SET_CURRENT_HOTEL(this.selectedHotelId);
        const selectedHotel = this.hotelList.find(
          (hotel) => hotel.hotelId === this.selectedHotelId
        );
        console.log("用户选择的酒店信息：", {
          酒店ID: selectedHotel.hotelId,
          酒店名称: selectedHotel.hotelName,
        });
        const redirect = this.$route.query.redirect || "/";
        router.push(redirect);
      }
    },
  },
  async mounted() {
    this.isLoading = true;
    try {
      await this.$store.dispatch("hotel/fetchHotelList");
    } catch (error) {
      console.error("Failed to load hotels:", error);
      this.$message.error("Could not load hotels. Please try again.");
    } finally {
      this.isLoading = false;
    }
  },
};
</script>

<style scoped lang="scss">
.hotel-select-page {
  min-height: 100vh;
  padding: 20px;
  font-family: "Inter Variable", sans-serif;
  color: #1e293b;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.page-header {
  width: 100%;
  max-width: 600px; /* 与下方盒子宽度一致 */
  margin-bottom: 30px;

  .logo-container {
    display: flex;
    align-items: center;
    width: 100%; /* 占满父容器宽度 */
    max-width: 600px; /* 与下方盒子宽度一致 */
    margin: 0 auto 30px; /* 居中并增加底部间距 */
  }

  .page-logo {
    width: 100%; /* 按比例放大 */
    max-width: 120px; /* 与整体比例协调 */
    height: auto; /* 保持图片比例 */
    object-fit: contain; /* 防止图片变形 */
  }

  .page-title {
    width: 100%; /* 占满剩余宽度 */
    margin-left: 20px;
    font-size: 28px;
    font-weight: 700;
    color: #1e40af;

    img {
      width: 100% !important; /* 强制与父容器同宽 */
      height: auto !important; /* 保持比例 */
      max-width: 450px; /* 与整体比例协调 */
    }
  }
}

.page-container {
  display: flex;
  justify-content: center;
  width: 100%;
}

.form-wrapper {
  width: 100%;
  max-width: 600px; /* 基准宽度 */
}

.form-card {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1),
    0 8px 10px -6px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.form-header {
  padding: 30px 30px 20px;
  border-bottom: 1px solid #f1f5f9;

  .header-decoration {
    width: 40px;
    height: 4px;
    background-color: #3b82f6;
    border-radius: 2px;
    margin-bottom: 15px;
  }

  .form-title {
    font-size: 22px;
    font-weight: 600;
    color: #1e40af;
    margin: 0 0 8px 0;
  }

  .form-subtitle {
    font-size: 14px;
    color: #64748b;
    margin: 0;
  }
}

.form-body {
  padding: 30px;
  min-height: 300px;
  display: flex;
  flex-direction: column;
}

.loading-state {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
  color: #64748b;

  i {
    margin-right: 10px;
    font-size: 20px;
  }
}

.hotel-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 10px;
  flex: 1;
}

.hotel-item {
  display: grid;
  grid-template-columns: 0px 1fr 50px;
  align-items: center;
  padding: 15px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;

  &:hover {
    border-color: #93c5fd;
    background-color: #f8fafc;
  }

  &.active {
    border-color: #3b82f6;
    background-color: #eff6ff;
  }

  .hotel-icon {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background-color: #eff6ff;
    color: #3b82f6;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 15px;
  }

  .hotel-info {
    .hotel-name {
      font-size: 17px;
      font-weight: 600;
      color: #1e293b;
      margin-bottom: 10px;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }

    .hotel-address {
      font-size: 13px;
      font-weight: 400;
      color: #475569;
      margin-bottom: 10px;
      line-height: 1.4;
      max-width: 100%;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
    }

    .hotel-details {
      display: flex;
      flex-wrap: wrap;
      gap: 15px;
      font-size: 12px;
      color: #64748b;

      .detail-item {
        display: flex;
        align-items: center;
        font-weight: 400;

        img,
        i {
          margin-right: 5px;
          width: 14px;
          text-align: center;
        }
      }

      .star {
        color: #f59e0b;
      }
    }
  }

  .select-checkbox {
    width: 20px;
    height: 20px;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    justify-self: center;

    &.checked {
      background-color: #3b82f6;
      border-color: #3b82f6;
      color: white;
    }

    i {
      font-size: 14px;
    }
  }
}

.form-footer {
  padding: 20px 30px;
  border-top: 1px solid #f1f5f9;
  background-color: #f8fafc;

  .submit-btn {
    width: 100%;
    padding: 14px;
    background-color: #3b82f6;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;

    &:hover {
      background-color: #2563eb;
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(59, 130, 246, 0.2);
    }

    &:disabled {
      background-color: #bfdbfe;
      cursor: not-allowed;
      transform: none;
      box-shadow: none;
    }
  }
}

.help-link {
  text-align: center;
  margin-top: 20px;

  a {
    color: #3b82f6;
    font-size: 14px;
    text-decoration: none;

    &:hover {
      text-decoration: underline;
      color: #2563eb;
    }
  }
}

@media (max-width: 600px) {
  .page-header {
    .page-logo {
      max-width: 80px; /* 响应式调整 */
    }

    .page-title {
      font-size: 24px;
      width: auto;
    }
  }

  .form-header,
  .form-body,
  .form-footer {
    padding: 20px;
  }

  .hotel-item {
    grid-template-columns: 40px 1fr 40px;
  }

  .hotel-info {
    .hotel-name {
      font-size: 16px;
    }
    .hotel-address {
      font-size: 12px;
      -webkit-line-clamp: 3;
    }
  }

  .hotel-details {
    gap: 8px;
  }
}
</style>
